﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Reader.Models;

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public string Description { get; set; }
    public string CoverImage { get; set; }
    public BookType Type { get; set; }
    public int UploadedUserId { get; set; }
    public string StoragePath { get; set; }
    public string MD5 { get; set; }
    public DateTime UploadTime { get; set; }
    
    public int CategoryId { get; set; }
    
    public int ReadCount { get; set; }
    public BookStatus Status { get; set; }
    public ICollection<Chapter> Chapters { get; set; }
    
    public BookCategory Category { get; set; }
    
    // 导航属性，表示书籍的上传用户
    public User UploadedBy { get; set; }
    // 枚举定义
    public enum BookType
    {
        TXT,
        PDF,
        // Add more book types here
    }

    public enum BookStatus
    {
        Updating,
        Completed
    }

    // 静态方法用于配置实体
    public static void Configure(EntityTypeBuilder<Book> builder)
    {
        builder.HasKey(b => b.Id);
        builder.Property(b => b.Id).ValueGeneratedOnAdd();
        // 配置Chapters导航属性
        builder.HasMany(b => b.Chapters)
            .WithOne(c => c.Book)
            .HasForeignKey(c => c.BookId);
        // 配置UploadedUserId外键关系
        builder.HasOne(b => b.UploadedBy)
            .WithMany(u => u.UploadedBooks)
            .HasForeignKey(b => b.UploadedUserId);
        // 配置CategoryId外键关系
        builder.HasOne(b => b.Category)
            .WithMany(c => c.Books)
            .HasForeignKey(b => b.CategoryId);
    }
}



public class BookUploadModel
{
    [Required(ErrorMessage = "标题是必填项。")]
    [StringLength(255, ErrorMessage = "标题不能超过255个字符。")]
    public string Title { get; set; }

    [Required(ErrorMessage = "作者是必填项。")]
    [StringLength(255, ErrorMessage = "作者不能超过255个字符。")]
    public string Author { get; set; }

    [Required(ErrorMessage = "描述是必填项。")]
    [StringLength(1000, ErrorMessage = "描述不能超过1000个字符。")]
    public string Description { get; set; }

    public string CoverImage { get; set; }

    [Required(ErrorMessage = "类型是必填项。")]
    public Book.BookType Type { get; set; }

    [Required(ErrorMessage = "书籍文件是必填项。")]
    public IFormFile StoragePath { get; set; }

    [Required(ErrorMessage = "状态是必填项。")]
    public Book.BookStatus Status { get; set; }

    [Required(ErrorMessage = "分类是必填项目")]
    public int Category { get; set; }
    
    
}
